package com.example.firstapp.network

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import androidx.activity.result.contract.ActivityResultContracts
import com.example.firstapp.R
import com.example.firstapp.databinding.ActivityNetworkBinding

class NetworkActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_network)

        val binding = ActivityNetworkBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val resultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
            if (result.resultCode  == RESULT_OK) {
                val returnData = result.data?.getStringExtra("return_data")
                val ed: TextView = findViewById(R.id.tv_result);  // 替换为你TextView的id
                ed.append(returnData)
            }
        }
        binding.HttpURLConnection.setOnClickListener {
            val url = binding.url.text.toString()
            val intent = Intent(this, HttpURLConnectionActivity::class.java)
            intent.putExtra("url", url)
            // 2、期望新启动Activity在销毁的时候能够返回一个结果
            // startActivityForResult(intent, 1)  已启用
            startActivity(intent)
        }
        binding.OKhttp.setOnClickListener {
            val url = binding.url.text.toString()
            val intent = Intent(this, OkHttpActivity::class.java)
            intent.putExtra("url", url)
            startActivity(intent)
        }
        binding.pull.setOnClickListener {
            val url = binding.url.text.toString()
            val intent = Intent(this, PullActivity::class.java)
            intent.putExtra("url", url)
            startActivity(intent)
        }
    }
}